package com.ruoyi.patrol.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.ruoyi.common.domain.PtPolice;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SenseVoiceParser;
import com.ruoyi.patrol.domain.PtSchool;
import com.ruoyi.patrol.service.IPtSchoolService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.patrol.domain.PtPoliceRecord;
import com.ruoyi.patrol.service.IPtPoliceRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报警记录Controller
 *
 * @author lkm
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/patrol/record")
public class PtPoliceRecordController extends BaseController
{
    @Autowired
    private IPtPoliceRecordService ptPoliceRecordService;

    @Autowired
    private IPtSchoolService ptSchoolService;

    private int PtPoliceRecordListSize = 0;

    /**
     * 查询报警记录列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtPoliceRecord ptPoliceRecord)
    {
        ptPoliceRecord.setSchoolId(SecurityUtils.getSchoolId());
        startPage();
        List<PtPoliceRecord> list = ptPoliceRecordService.selectPtPoliceRecordList(ptPoliceRecord);
        return getDataTable(list);
    }

    /**
     * 导出报警记录列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:record:export')")
    @Log(title = "报警记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtPoliceRecord ptPoliceRecord)
    {
        List<PtPoliceRecord> list = ptPoliceRecordService.selectPtPoliceRecordList(ptPoliceRecord);
        ExcelUtil<PtPoliceRecord> util = new ExcelUtil<PtPoliceRecord>(PtPoliceRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 获取报警记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ptPoliceRecordService.selectPtPoliceRecordById(id));
    }

    /**
     * 新增报警记录
     */
    @PreAuthorize("@ss.hasPermi('patrol:record:add')")
    @Log(title = "报警记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtPoliceRecord ptPoliceRecord)
    {
        return toAjax(ptPoliceRecordService.insertPtPoliceRecord(ptPoliceRecord));
    }

    /**
     * 修改报警记录
     */
    @PreAuthorize("@ss.hasPermi('patrol:record:edit')")
    @Log(title = "报警记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtPoliceRecord ptPoliceRecord)
    {
        return toAjax(ptPoliceRecordService.updatePtPoliceRecord(ptPoliceRecord));
    }

    /**
     * 删除报警记录
     */
    @PreAuthorize("@ss.hasPermi('patrol:record:remove')")
    @Log(title = "报警记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ptPoliceRecordService.deletePtPoliceRecordByIds(ids));
    }

    /**
     * 接收报警器推送过来的报警记录
     */
    @PostMapping("/receive")
    public Map<String, Object> receive(@RequestBody PtPoliceRecord ptPoliceRecord)
    {
        if (ptPoliceRecord == null) {
            logger.error("");
            return error("报警记录为空");
        }
        if (ptPoliceRecordService.selectPtPoliceRecordByTaskid(ptPoliceRecord.getTaskId()) != null) {
            logger.error("报警记录已经存在");
            return error("报警记录已经存在");
        }
        PtSchool ptSchool = ptSchoolService.selectPtSchoolBySchoolName(ptPoliceRecord.getSchoolName());
        if (ptSchool == null) {
            logger.error("该学校不存在");
            return error("该学校不存在");
        }
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        gson.toJson(SenseVoiceParser.parseSenseVoiceResult(new JsonObject(),CPtPoliceRecord(ptPoliceRecord),true,true,true));
        ptPoliceRecord.setSchoolId(ptSchool.getId());
        ptPoliceRecordService.insertPtPoliceRecord(ptPoliceRecord);
        return success();
    }

    /**
     * 如果错误会调用改方法向硬件返回错误
     * */
    private static Map<String, Object> error(String msg) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("data", "500");
        ret.put("succ", false);
        ret.put("ts", System.currentTimeMillis());
        ret.put("code", "500");
        ret.put("msg", msg);
        return ret;
    }



    /**
     * 如果正确就调用该方法返回给硬件
     * */
    private static Map<String, Object> success() {
        Map<String, Object> ret = new HashMap<>();
        ret.put("data", "200");
        ret.put("succ", true);
        ret.put("ts", System.currentTimeMillis());
        ret.put("code", "200");
        ret.put("msg", "接收成功");
        return ret;
    }



    private void HttpClient (String SS){}
    private PtPolice CPtPoliceRecord(PtPoliceRecord ptPoliceRecord){
        PtPolice policeRecord = new PtPolice();
        return policeRecord;
    }

    /**
     * 比赛用的3D大屏
     * 查询是否有新的报警记录（且是“救命”）
     */
    @GetMapping("/isNewHelp")
    public boolean isNewHelp()
    {
        List<PtPoliceRecord> list = ptPoliceRecordService.selectPtPoliceRecordList(new PtPoliceRecord());
        int size = list.size();
        if (PtPoliceRecordListSize == 0){
            PtPoliceRecordListSize = list.size();
            return false;
        }
        if (PtPoliceRecordListSize == size){
            return false;
        }

        if (PtPoliceRecordListSize < size){
            PtPoliceRecord policeRecord = list.get(0);
            PtPoliceRecordListSize = list.size();
            return "救命".equals(policeRecord.getKeyword());
        }

        return false;
    }

    /**
     * 比赛时报警postman接口
     */
    @PostMapping("/receives")
    public String receives()
    {
        return "扂夔迒狟產薛奧祥夼旯蟾｡髻ｳ繝ｫ繧ｫ 鎴戣兘鍚炰笅鐜荤蟾｡髻ｳ繝ｫ繧ｫ 蟾｡拑鑰屼笉浼蟾｡髻ｳ繝ｫ繧ｫ よ蟾｡ 韩浣撱极и唰]U良τ" +
                "ぃ端ō砰C鎴戣兘鍚炰笅蟾｡髻ｳ繝ｫ繧ｫ 鐜荤拑鑰屼笉浼よ韩浣撱扂夔迒狟產薛奧祥夼旯蟾｡髻ｳ繝ｫ繧ｫ 鎴戣兘鍚炰笅鐜荤蟾｡髻ｳ繝ｫ繧ｫ 蟾｡拑鑰屼笉浼蟾｡髻ｳ繝ｫ繧ｫ よ蟾｡ 韩浣撱极и唰]U良τぃ端ō砰C鎴戣兘鍚炰笅蟾｡髻ｳ繝ｫ繧ｫ 鐜荤拑鑰屼笉浼よ韩浣撱扂夔迒狟產薛奧祥夼旯蟾｡髻ｳ繝ｫ繧ｫ 鎴戣兘鍚炰" +
                "笅鐜荤蟾｡髻ｳ繝ｫ繧ｫ 蟾｡拑鑰屼笉浼蟾｡髻ｳ繝ｫ繧ｫ よ蟾｡ 韩浣撱极и唰]U良τぃ端ō砰C鎴戣兘鍚" +
                "炰笅蟾｡髻ｳ繝ｫ繧ｫ 鐜荤拑鑰屼笉浼よ韩浣撱";
    }

}
